<template>
  <div class="userManagePage">
    <a-input-search
        style="width: 350px;margin-bottom: 20px"
        v-model:value="searchVal"
        placeholder="请输入用户名"
        enter-button="Search"
        size="large"
        @search="onSearch"
    />
    <a-table :columns="columns" :data-source="data">

      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'avatarUrl'">
          <a-image :src="record.avatarUrl" :width="120"/>
        </template>
        <template v-else-if="column.dataIndex === 'userRole'">
        <div v-if="record.userRole === 0">
          <a-tag color="blue">普通用户</a-tag>
        </div>
        <div v-else>
          <a-tag color="green">管理员</a-tag>
        </div>
        </template>
        <template v-else-if="column.dataIndex === 'createTime'">
          {{dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss')}}
        </template>
        <template v-else-if="column.key === 'action'">
        <a-button danger @click="doDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import {onMounted, ref} from "vue";
import {message} from "ant-design-vue";
import {reqSearchUser, reqUserDelete} from "@/api/user.ts";

import dayjs from 'dayjs'

const data = ref([]);
const searchVal = ref("");
const onSearch = async ()=>{
  const res = await reqSearchUser(searchVal.value);
  if(res.data.data){
    data.value = res.data.data;
  }else{
    message.error('获取用户列表失败')
  }
}
const doDelete = async (id:number)=>{
  const res = await reqUserDelete(id);
  if(res.data.code === 0){
    message.success('删除成功')
    fetchData();
  }else{
    message.error(`删除失败${res.data.message}`)
  }
}
const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '用户名',
    dataIndex: 'username',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '头像',
    dataIndex: 'avatarUrl',
  },
  {
    title: '性别',
    dataIndex: 'gender',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '操作',
    key: 'action',
  },
];


const fetchData = async ()=>{
  const res = await reqSearchUser("");
  if(res.data.data){
    data.value = res.data.data;
  }else{
    message.error('获取用户列表失败')
  }
}
onMounted(()=>{
  fetchData();
})
</script>

